﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using eWallet.Security.Cryptography;
using eWallet.Data.Local;

namespace eWallet
{
	internal static class CryptoModuleFactory
	{

		private static Dictionary<Guid, ICryptoModule> Modules = new Dictionary<Guid, ICryptoModule>();

		public static void ClearModule(eWalletDataSet.GroupsRow group)
		{
			if (Modules.ContainsKey(group.Id))
			{
				Modules.Remove(group.Id);
			}
		}

		public static ICryptoModule GetCryptoModule(eWalletDataSet.GroupsRow group)
		{

				if (Modules.ContainsKey(group.Id))
				{
					return Modules[group.Id];
				}

			CryptoModuleCreatorWindow dlg = new CryptoModuleCreatorWindow();
			dlg.CacheCredentials = Properties.Settings.Default.CacheCredentials;
			dlg.EncryptionGroup = group;
			var result = dlg.ShowDialog();
			if (result.HasValue && result.Value)
			{
				Properties.Settings.Default.CacheCredentials = dlg.CacheCredentials;
				Properties.Settings.Default.Save();

				if (dlg.CacheCredentials)
				{
					Modules[group.Id] = dlg.SelectedModule;
				}

				return dlg.SelectedModule;
			}

			return null;
		}

	}
}
